package com.wuyi.controller;

import com.github.pagehelper.PageInfo;
import com.wuyi.pojo.bo.PostBO;
import com.wuyi.pojo.bo.PostQueryBO;
import com.wuyi.pojo.vo.PostDetailsVO;
import com.wuyi.service.CenterUserService;
import com.wuyi.service.FileUploadService;
import com.wuyi.service.PostService;
import com.zc.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName PostController.java
 * @Description 帖子相关Controller
 * @createTime 2021年11月03日 11:23:00
 */
@Api(value = "帖子",tags = {"用于帖子的相关接口"})
@RestController
@RequestMapping("post")
public class PostController {

    public final static Logger logger = LoggerFactory.getLogger(PostController.class);

    @Autowired
    private PostService postService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private CenterUserService centerUserService;

//    @Autowired
//    private FileUploadUtils fileUploadUtils;


    /*@ApiOperation(value = "帖子列表查询", notes = "帖子列表查询", httpMethod = "GET")
    @GetMapping("/list")
    public JSONResult list(@RequestBody PostQueryBO postQueryBO) {


        return JSONResult.ok();
    }*/

//    @ApiOperation(value = "上传测试", notes = "上传测试", httpMethod = "POST")
//    @PostMapping("/testUpload")
//    public JSONResult testUpload(@RequestParam("file") MultipartFile file,
//                                 @RequestParam String userName) throws IOException {
//
//        String myObjectName = fileUploadService.upload(file, userName, "post");
//
//        return JSONResult.ok(myObjectName);
//    }
//
//    @ApiOperation(value = "下载测试", notes = "下载测试", httpMethod = "GET")
//    @GetMapping("/testDownload")
//    public void testDownload(@RequestParam String path, HttpServletResponse response) throws IOException {
//
//        // 浏览器以附件形式下载
//        this.fileUploadService.download(path, response);
//    }




    @ApiOperation(value = "新增帖子（已对接）", notes = "新增帖子（已对接）", httpMethod = "POST")
    @PostMapping("/addPost")
    public JSONResult addPost(@ApiParam(name = "postTitle", value = "帖子标题", required = true)
                          @RequestParam String postTitle,
                          @ApiParam(name = "postText", value = "帖子描述", required = true)
                          @RequestParam String postText,
                          @ApiParam(name = "userName", value = "发帖用户", required = true)
                          @RequestParam String userName,
                          @ApiParam(name = "isAnonymous", value = "是否匿名", required = false)
                          @RequestParam Integer isAnonymous,
                          @ApiParam(name = "images", value = "图片文件", required = false)
                          @RequestParam("images") MultipartFile[] images) throws IOException {
        //判空操作
        if (StringUtils.isBlank(postTitle)) {
            return JSONResult.errorMsg("标题不能为空");
        }
        if (StringUtils.isBlank(postText)) {
            return JSONResult.errorMsg("描述不能为空");
        }
        if (images.length > 8) {
            return JSONResult.errorMsg("图片数量过多，请删减至8张以下");
        }
        String fileType = "";
        if (images.length > 0) {
            //若有一个不为图片格式，则报错
            for (MultipartFile image : images) {
                String filesName = image.getOriginalFilename();
                //截取后缀名
                int fileOf = filesName.lastIndexOf(".");
                fileType = filesName.substring(fileOf + 1);
                if (fileType.equals("jpg") || fileType.equals("png") || fileType.equals("jpeg") || fileType.equals("")) {
                    logger.info("当前文件的格式为：" + fileType);
                }else{
                    return JSONResult.errorMsg("图片格式不正确");
                }
            }
        }

        PostBO postBO = new PostBO();
        postBO.setPostUid(userName);
        postBO.setPostTitle(postTitle);
        postBO.setPostText(postText);
        postBO.setIsAnonymous(isAnonymous);
        if ("".equals(fileType)) {
            postBO.setPostImage(null);
        }else{
            postBO.setPostImage(images);
        }

        return postService.addPost(postBO);
    }


    @ApiOperation(value = "按帖子id查询帖子详情（已对接）", notes = "按帖子id查询帖子详情（已对接）", httpMethod = "GET")
    @GetMapping("/details")
    public JSONResult details(@ApiParam(name = "postId", value = "帖子Id", required = true)
                              @RequestParam Integer postId) {

        if (null == postId) {
            return JSONResult.errorMsg("帖子Id不能为空");
        }
        PostDetailsVO postDetailsVO = postService.selectPostDetails(postId);

        return JSONResult.ok(postDetailsVO);
    }



    @ApiOperation(value = "帖子页面根据用户Id查询个人信息（已对接）", notes = "帖子页面根据用户Id查询个人信息（已对接）", httpMethod = "GET")
    @GetMapping("/queryUserInfo")
    public JSONResult queryUserInfo(@ApiParam(name = "userId", value = "点击用户的id", required = true)
                                    @RequestParam String userId,
                                    @ApiParam(name = "userName", value = "当前登录的userName", required = true)
                                    @RequestParam String userName) {

        if (null == userId) {
            return JSONResult.errorMsg("userId不能为空");
        }


        return JSONResult.ok(postService.queryUserInfo(userId, userName));
    }


    @ApiOperation(value = "查询个人发布的帖子（已对接）", notes = "查询个人发布的帖子（已对接）", httpMethod = "GET")
    @GetMapping("/queryPostList")
    public JSONResult queryPostList(@ApiParam(name = "userId", value = "用户id", required = true)
                                    @RequestParam String userId,
                                    @ApiParam(name = "pageNum", value = "第几页", required = true)
                                    @RequestParam( required = false,defaultValue = "1")Integer pageNum,
                                    @ApiParam(name = "pageSize", value = "一页多少条数据", required = true)
                                    @RequestParam( required = false,defaultValue = "10")Integer pageSize) {

        if (null == userId) {
            return JSONResult.errorMsg("userId不能为空");
        }
        if (null == pageNum) pageNum = 1;
        if (null == pageSize) pageSize = 10;

        PageInfo pageInfo = postService.queryPostList(userId, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }



    @ApiOperation(value = "删除帖子（已对接）", notes = "删除帖子（已对接）", httpMethod = "POST")
    @PostMapping("/delete")
    public JSONResult delete(@ApiParam(name = "postId", value = "帖子Id", required = true)
                             @RequestParam Integer postId) {

        if (null == postId) {
            return JSONResult.errorMsg("帖子失效");
        }
        postService.deletePost(postId);
        return JSONResult.ok();
    }


}
